package com.hope.hotel.controller;

import com.hope.hotel.common.Result;
import com.hope.hotel.entity.po.Rooms;
import com.hope.hotel.service.IRoomsService;
import com.hope.hotel.utils.AliOSSUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  客房管理控制器
 * </p>
 *
 * @author LH
 * @since 2025-05-30
 */
@RestController
@RequestMapping("/rooms")
@RequiredArgsConstructor
public class RoomsController {

    private final IRoomsService roomsService;

    private final AliOSSUtils aliOSSUtils;

    /**
     * 获取客房列表
     */
    @GetMapping("/list")
    public Result getRoomsList(@RequestParam Long hotelId,
                              @RequestParam(defaultValue = "1") int page, 
                              @RequestParam(defaultValue = "10") int pageSize) {
        return roomsService.getRoomsByHotelId(hotelId, page, pageSize);
    }

    /**
     * 获取客房详情
     */
    @GetMapping("/{id}")
    public Result getRoomDetail(@PathVariable Long id) {
        return roomsService.getRoomById(id);
    }

    /**
     * 添加客房
     */
    @PostMapping("/add")
    public Result addRoom(@RequestBody Rooms room) {
        // 确保room对象中包含hotelId
        if (room.getHotelId() == null) {
            return Result.error("酒店ID不能为空");
        }
        return roomsService.addRoom(room);
    }

    /**
     * 更新客房
     */
    @PutMapping("/update")
    public Result updateRoom(@RequestBody Rooms room) {
        // 确保room对象中包含hotelId
        if (room.getHotelId() == null) {
            return Result.error("酒店ID不能为空");
        }
        return roomsService.updateRoom(room);
    }

    /**
     * 删除客房
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteRoom(@PathVariable Long id) {
        return roomsService.deleteRoom(id);
    }

    /**
     * 上传照片
     */
    @PostMapping("/upload-image")
    public Result uploadAvatar(@RequestParam("file") MultipartFile image) {
        try {
            if (image == null || image.isEmpty()) {
                return Result.error("上传文件不能为空");
            }
            String url = aliOSSUtils.upload(image);
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败: " + e.getMessage());
        }
    }
}
